/*
* EP-Tracing System : a tool to trace mountain hike.           .kNNx
* Copyright (C) 2009  Raffaello Brondi                         'KNNNk
*                                                             ,XNNNl
* This program is free software:                             ,XNN0'
* you can redistribute it and/or modify                     ,XNNx.
* it under the terms of the                                'XNNl
* GNU General Public License                              .KNN:                     ..
* as published by                                        .0NNo                    .ONNO.
* the Free Software Foundation,                          xNNN.  :dkd,            .KNNNN;
* either version 3 of the License, or                   :NNNK ;XNNNNNo          ,KNNNX:
* (at your option) any later version.                   xNNNNcXNNNNNNk         cNNNNx.
*                                                       ,XNNNNNNNNNNX'       .xNNNK;
*                                                        .lKNNNNNNNK'       :KNNNo
*                                                          ;kNNNNNKxl;'.'l0NNNk.
* This program is distributed in                          .'  .:xXNNNNNNNNNNNO,
* the hope that it will be useful,                       :NNXd'   ,oONNNNNNk,
* but WITHOUT ANY WARRANTY;                              xNNNNN0c.    .;;,.
* without even the implied warranty of                   kNNNNNNNXd'
* MERCHANTABILITY or                                     xNNNNNNNNNNOc.
* FITNESS FOR A PARTICULAR PURPOSE.                      xNNNNNNNNNNNNXd.
* See the GNU General Public License                     ONNNN0.,o0NNNNNNl
* for more details.                                      0NNNNd    kNNNNNX.
*                                                        KNNNNl   oNNNNNXc
*                                                       .XNNNN:  dNNNNNx.
* You should have received a copy                       .XNNNN; lNNNNO'
* of the GNU General Public License                     .XNNNN; 'dxo'
* along with this program.                              .XNNNN;
* If not, see <http://www.gnu.org/licenses/>.            0NNNN:
*                                                        kNNNNc
*                                                        oNNNNd
*                                                        ;NNNN0
* Contact: rbrondi@gmail.com                             .XNNNN;
*                                                         kNNNNd
*                                                         .coo:.
*/

package EP.UI.Trace;

import java.util.Timer;
import java.util.TimerTask;
/**
 *
 * @author Raffaello Brondi
 */
public class Chronometer{
    private Timer timer;
    private Time current;

    private GPSUI parent;

    public Chronometer(GPSUI p){
        this.parent = p;
        this.timer = new Timer();
        this.current = new Time();
    }

    public void start(){
        this.timer.scheduleAtFixedRate(new TimerTask() {
            public void run() {
                current.updateTime();
                parent.setChronometerTime(current.toString());
            }
        }, 0, 1000);
    }

    public class Time{
        private byte seconds;
        private byte minutes;
        private byte hours;

        public Time(){
            this.hours = 0;
            this.minutes = 0;
            this.seconds = 0;
        }

        public void updateTime(){
            if(this.seconds < 59)
                this.seconds++;
            else{
                this.seconds = 0;
                if(this.minutes < 59)
                    this.minutes++;
                else{
                    this.minutes = 0;
                    if(this.hours < 23)
                        this.hours++;
                    else
                        this.hours = 0;
                }
            }
        }

        public String toString(){
            StringBuffer time = new StringBuffer();

            time.append(this.hours + ":");
            if(this.minutes > 9)
                time.append(this.minutes + ":");
            else
                time.append("0" + this.minutes + ":");

            if(this.seconds > 9)
                time.append(this.seconds);
            else
                time.append("0" + this.seconds);

            return time.toString();
        }
    }

}
